﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using Microsoft.Win32;

namespace PerformanceTestsFile
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly ObservableCollection<string> _lines = new ObservableCollection<string>();


        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = _lines;
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _lines.Clear();

            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == true)
            {
                byte[] buffer = File.ReadAllBytes(dlg.FileName);
                string s1 = String.Empty, s2 = String.Empty, s3 = String.Empty;

                for (int i = 0; i < 6; i++)
                {
                    Benchmark b1 = new Benchmark("Base64 Time", _lines);
                    s1 = Convert.ToBase64String(buffer, 0, buffer.Length);
                    b1.Dispose();
                    TimeSpan ts1 = b1.Elapsed;

                    Benchmark b2 = new Benchmark("Base85P5 Compressed Time", _lines);
                    s2 = Base85P5.Base85P5.EncodeAndCompress(buffer);
                    b2.Dispose();
                    TimeSpan ts2 = b2.Elapsed;

                    Benchmark b3 = new Benchmark("Base85P5 Not Compressed Time", _lines);
                    s3 = Base85P5.Base85P5.Encode(buffer);
                    b3.Dispose();
                    TimeSpan ts3 = b3.Elapsed;


                    long timesSlower = ts2.Ticks / ts1.Ticks;
                    _lines.Add($"Base85P5 Compressed slower than Base64 {timesSlower}");
                    timesSlower = ts3.Ticks / ts1.Ticks;
                    _lines.Add($"Base85P5 Not Compressed slower than Base64 {timesSlower}");
                    _lines.Add("");
                }


                _lines.Add("======================================================================");
                double perc1 = s1.Length * 100.0 / buffer.Length;
                double perc2 = s2.Length * 100.0 / buffer.Length;
                double perc3 = s3.Length * 100.0 / buffer.Length;
                _lines.Add($"Base64 Length {s1.Length}, increased by {perc1}%");
                _lines.Add($"Base85P5 Compressed Length {s2.Length}, increased by {perc2}%");
                _lines.Add($"Base85P5 Not Compressed Length {s3.Length}, increased by {perc3}%");
            }
        }
    }
}
